package com.gnerv.pabu.basic.core.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.pabu.basic.core.utils.UUIDUtils;
import com.gnerv.pabu.basic.core.dto.ArticleDTO;
import com.gnerv.pabu.basic.core.dto.ArticleDetailsDTO;
import com.gnerv.pabu.basic.core.dto.ArticleSaveDTO;
import com.gnerv.pabu.basic.core.dto.ArticleSearchDTO;
import com.gnerv.pabu.basic.core.entity.Article;
import com.gnerv.pabu.basic.core.entity.ArticleDetails;
import com.gnerv.pabu.basic.core.mapper.ArticleDetailsMapper;
import com.gnerv.pabu.basic.core.mapper.ArticleMapper;
import com.gnerv.pabu.basic.core.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Resource
    ArticleMapper articleMapper;

    @Resource
    ArticleDetailsMapper articleDetailsMapper;

    @Override
    public boolean saveArticle(ArticleSaveDTO articleSaveDTO) {
        if(StringUtils.isEmpty(articleSaveDTO.getUkId())){
            createArticle(articleSaveDTO);
        }else {
            updateArticle(articleSaveDTO);
        }
        return true;
    }

    @Override
    public boolean createArticle(ArticleSaveDTO articleSaveDTO) {
        articleSaveDTO.setUkId(UUIDUtils.getLower());
        articleMapper.insert(articleSaveDTO.convert());
        articleDetailsMapper.insert(articleSaveDTO.convertArticleDetails());
        articleMapper.bindCatalogArticle(articleSaveDTO.getCatalogId(), articleSaveDTO.getUkId());
        return true;
    }

    @Override
    public boolean deleteByArticleId(String articleId) {
        articleMapper.deleteById(articleId);
        articleDetailsMapper.deleteById(articleId);
        articleMapper.unBindCatalogArticle(articleId);
        return true;
    }

    @Override
    public boolean updateArticle(ArticleSaveDTO articleSaveDTO) {
        articleMapper.updateById(articleSaveDTO.convert());
        articleDetailsMapper.updateById(articleSaveDTO.convertArticleDetails());
        articleMapper.unBindCatalogArticle(articleSaveDTO.getUkId());
        articleMapper.bindCatalogArticle(articleSaveDTO.getCatalogId(), articleSaveDTO.getUkId());
        return true;
    }

    @Override
    public boolean updateArticleStatus(String articleId, Integer status) {
        articleMapper.updateArticleStatus(articleId, status);
        return true;
    }

    @Override
    public ArticleDTO selectArticle(String articleId) {
        ArticleDTO article = articleMapper.selectArticle(articleId);
        return article;
    }

    @Override
    public ArticleDetailsDTO selectArticleDetails(String articleId) {
        ArticleDetails articleDetails = articleDetailsMapper.selectById(articleId);
        ArticleDetailsDTO articleDetailsDTO = new ArticleDetailsDTO();
        BeanUtils.copyProperties(articleDetails, articleDetailsDTO);
        return articleDetailsDTO;
    }

    @Override
    public Page<ArticleDTO> listArticle(ArticleSearchDTO article, Long current, Long size) {
        Page<ArticleDTO> articles = articleMapper.listArticle(new Page<ArticleDTO>(current, size), article);
        return articles;
    }
}
